package com.tanhua.dubbo.api;


import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Focus;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class FocusApiImpl implements FocusApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean queryFocuslike(Long userId, String publishId, int commentType) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(publishId))
        .and("userId").is(userId)
        .and("commentType").is(commentType));
        return mongoTemplate.exists(query, Focus.class);
    }

    @Override
    public Integer saveFocuslike(Focus focus) {
        //往focus表添加点赞数据，更新video表的点赞数返回
        //获取发布视频的人
        Video video = mongoTemplate.findById(focus.getPublishId(), Video.class);
        if (video != null){
            focus.setPublishUserId(video.getUserId());
        }
        mongoTemplate.save(focus);

        //更新video数据
        Query query =Query.query(Criteria.where("id").is(video.getId()));
        Update update = new Update();
        if (focus.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (focus.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //更新数据
        //设置更新
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);
        //获取更新后的最新参数
        Video andModify = mongoTemplate.findAndModify(query, update, findAndModifyOptions, Video.class);

        return andModify.statisCount(focus.getCommentType());
    }

    @Override
    public Integer deleteFocusdislike(Focus focus) {
        //往focus表删除点赞数据，更新video表的点赞数-1返回
        Query query = Query.query(Criteria.where("publishId").is(focus.getPublishId())
                .and("userId").is(focus.getUserId())
                .and("commentType").is(focus.getCommentType()));
        mongoTemplate.remove(query,Focus.class);
        //更新video表的点赞数-1返回
        Query query1 = Query.query(Criteria.where("id").is(focus.getPublishId()));
        Update update = new Update();
        if (focus.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (focus.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        //设置更新
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);
        //获取更新后的最新参数
        Video andModify = mongoTemplate.findAndModify(query1, update, findAndModifyOptions, Video.class);

        return andModify.statisCount(focus.getCommentType());
    }

    @Override
    public List<Focus> queryFocus(String publishId, Integer page, Integer pagesize, int type) {
        Criteria criteria = Criteria.where("publishId")
                .is(new ObjectId(publishId))
                .and("commentType")
                .is(type);
        Query query = new Query(criteria).skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Focus> foci = mongoTemplate.find(query, Focus.class);
        return foci;
    }

    @Override
    public boolean queruFocusCommentsLike(Focus focus) {
        Query query = Query.query(Criteria.where("publishId").is(focus.getPublishId())
        .and("commentType").is(focus.getCommentType())
        .and("userId").is(focus.getUserId()));
        return mongoTemplate.exists(query, Focus.class);
    }

    @Override
    public Integer saveFocusCommentsLike(Focus focus) {
        //根据前端传来的评论ID 更新评论点赞数
        Query query = Query.query(Criteria.where("id").is(focus.getPublishId()));
        Update update = new Update();
        if (focus.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",+1);
        }
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);
        Focus andModify = mongoTemplate.findAndModify(query, update,findAndModifyOptions,Focus.class);
        //拿出评论userid 封装到focus
        focus.setPublishUserId(andModify.getUserId());
        mongoTemplate.save(focus);
        return andModify.getLikeCount();
    }

    @Override
    public Integer DeleteFocusCommentsLike(Focus focus) {
        //删除foucs里面本条点赞的数据，并且更新评论
        Query query =Query.query(Criteria.where("publishId").is(focus.getPublishId())
        .and("commentType").is(focus.getCommentType())
        .and("userId").is(focus.getUserId()));
        mongoTemplate.remove(query,Focus.class);

        //更新评论点赞数
        Query query1 = Query.query(Criteria.where("id").is(focus.getPublishId()));
        Update update = new Update();
        if (focus.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);
        Focus andModify = mongoTemplate.findAndModify(query1, update, findAndModifyOptions, Focus.class);
        return andModify.getLikeCount();
    }
}
